﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sample.Messages;
using SimpleServiceBus.Bus.MessageManagement.MessageHandling;
using System.Drawing;
using System.IO;

namespace SampleConsumer.Handlers {
    public class PictureHandler: IMessageHandler<PictureMessage> {

        /* Note that this handler can use the IPictureService because it was registered in the endpoint like this:
                .SetupLocalServices(x => {
                    x.Register<IPictureService>(this); 
                })
         */

        public void Handle(PictureMessage message, SimpleServiceBus.Bus.Pipeline.IMessageContext context) {
            Logger.Log("Received picture {0}, {1} bytes", message.FileName, message.Bytes.Length);
            Logger.Log("  Comments: {0}", String.Join(", ", message.Comments));
            using(var _stream = new MemoryStream(message.Bytes)) {
                var _picture = Image.FromStream(_stream);
                var _service = context.GetLocalService<IPictureService>();
                if(_service != null)
                    _service.Display(message.FileName, _picture);
            }
        }

    }
}
